#!/usr/bin/python
inf = "load.inp"
outf = "userdef.h"
ofile = open(outf, 'w')
file_obj = open(inf, 'r')
ofile.writelines("#ifndef __User\n")
ofile.writelines("#define __User\n")
ofile.writelines("#include \"physicalconstants.h\"\n")
mpiflag = 0
ompflag = 0
prgname = ""
for line in file_obj:
    if '#' in line:
        continue
    if line != '' and '=' in line and "#" not in line and "mpi" not in line and "omp" not in line:
        print "setting ", line
        var = line.split('=')
        if var[0].strip() == "program":
            print 'var 0', var[0], var[-1]
            prgname = var[-1].strip()
        else:
            ofile.writelines("#define " + var[0] + " " + var[-1])
    if "use_mpi" in line:
        if "1" in line:
            mpiflag = 1
            print "will use mpi"
            ofile.writelines("#define __MPI\n")
        else:
            print "will run in seriel mode without MPI support"
    if "omp" in line:
        if "1" in line:
            ompflag = 1
            print "will use omp"
            ofile.writelines("#define __OMP\n")
        else:
            print "will run in seriel mode without omp support"

ofile.writelines("#endif")
file_obj.close()
ofile.close()
outf = "compile.sh"
ofile = open(outf, 'w')
print "will generat ", prgname
if mpiflag:
    ofile.writelines("#!/bin/bash\n")
    if ompflag:
        ofile.writelines("mpic++ main.cpp -o " + prgname + " -fopenmp -O3 -fPIC -std=c++11\n")
    else:
        ofile.writelines("mpic++ main.cpp -o " + prgname + " -O3 -fPIC -std=c++11\n")
else:
    ofile.writelines("#!/bin/bash\n")
    if ompflag:
        ofile.writelines("g++ main.cpp -o " + prgname + " -fopenmp -O3 -fPIC -std=c++11\n")
    else:
        ofile.writelines("g++ main.cpp -o " + prgname + " -O3 -fPIC -std=c++11\n")
